package vip.xiaonuo.modular.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import vip.xiaonuo.modular.entity.BasGpsExec;
import vip.xiaonuo.modular.entity.GpsInfo;
import vip.xiaonuo.modular.entity.dto.QueryGPSInfoDTO;
import vip.xiaonuo.modular.entity.request.QueryGPSInfoRequest;
import vip.xiaonuo.modular.entity.response.Response;
import vip.xiaonuo.modular.mapper.GpsInfoMapper;
import vip.xiaonuo.modular.service.GpsInfoService;
import vip.xiaonuo.modular.util.CommonException;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Author tuchaoren
 * @Date 2021-07-01
 **/
@Service
@Slf4j
public class GpsInfoServiceImpl extends ServiceImpl<GpsInfoMapper, GpsInfo> implements GpsInfoService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //    @Value("${queryGPSInfoUrl}")
    private String queryGPSInfoUrl = "http://39.108.3.206:9005/api/positions/truck";

    private final String asyncInsertShipment = "ASYNC:INSERT:WAYBILL:";

    @Override
    @Async(value = "insertExecutor")
    public void batchInsertGpsInfo(List<GpsInfo> gpsInfo) {
        // 异步新增获取的gps信息
        gpsInfo.forEach(data -> {
            if (!this.insertGpsInfo(data)) {
                log.info("新增失败！");
            }
        });
    }

    private boolean insertGpsInfo(GpsInfo gpsInfo) {
        return super.save(gpsInfo);
    }

    @Override
    public List<GpsInfo> queryGpsInfo(QueryGPSInfoDTO gpsInfoDTO) {
        LambdaQueryWrapper<GpsInfo> lambdaQuery = Wrappers.<GpsInfo>lambdaQuery()
                .like(GpsInfo::getVehicleNo, gpsInfoDTO.getVehicleNo());
        return super.list(lambdaQuery);
    }




    @Override
    public List<BasGpsExec> getInterfaceGPS(QueryGPSInfoRequest gpsInfoRequest) {
        /*Map<String, Object> param = new HashMap<>();
        param.put("cusId", gpsInfoRequest.getTruckId());
        param.put("licensePlate", gpsInfoRequest.getTruckId());
        param.put("trainno", gpsInfoRequest.getTruckId());
        param.put("gpsId", gpsInfoRequest.getTruckId());
        param.put("startTime", gpsInfoRequest.getStartTime());
        param.put("endTime", gpsInfoRequest.getEndTime());*/
        log.info("获取gps信息请求参数 -> {}", JSONUtil.toJsonStr(gpsInfoRequest));
        // TODO 获取时 第三方结束时间是小于 还是小于等于 如果是小于等于需要调整为小于
        String result = HttpUtil.get(queryGPSInfoUrl, JSONUtil.toBean(JSONUtil.toJsonStr(gpsInfoRequest), Map.class));
        log.info("获取的gps信息 -> {}", result);

        // 信息对象转换
        Response<List<BasGpsExec>> parse = JSONObject.parseObject(result, new TypeReference<Response<List<BasGpsExec>>>(){}.getType());
//        log.info("获取的gps信息 -> {}", JSON.toJSONString(parse));
        if (parse == null && !"200".equals(parse.getCode())) {
            log.error("请求响应信息 -> " + JSON.toJSONString(parse));
            throw new CommonException("获取gps信息异常！");
        }

        return parse.getData();
    }



}
